//
//  JSTTabbarViewController.swift
//  MoMo
//
//  Created by 高得华 on 2019/9/22.
//  Copyright © 2020 华&梅. All rights reserved.
//

import UIKit

class JSTTabbarViewController: MDHTabBarController, UITabBarControllerDelegate {
    
    ///重写父类的属性
    override var viewModel: JSTTabbarViewModel {
        get {
            return super.viewModel as! JSTTabbarViewModel
        }
    }
    
    /// viewModel
    override func viewDidLoad() {
        super.viewDidLoad()
        /// 初始化所有的子控制器
        self.setupAllChildViewController()
        /// set delegate
        self.tabBarController.delegate = self
    }
    
    /// 初始化所有的子控制器
    fileprivate func setupAllChildViewController() -> () {
        let titlesArray = ["Word", "PDF", ]//
        let imageNamesArray = ["tabbar_home", "tabbar_dongtai", ]
        var tabbarItems = [[String: String]]()
        var navs = [MDHNavigationController]()
        ///视图控制器
        let vcs = ["JSTDocListViewController", "JSTDocListViewController", ]
        for (i, title) in titlesArray.enumerated() {
            let im = imageNamesArray[i]
            let imSel = im+"_sel"
            let item = [CYLTabBarItemTitle: title,
                        CYLTabBarItemImage: im,
                        CYLTabBarItemSelectedImage: imSel,]
            tabbarItems.append(item)
            
            ///
            let vc = MDHSwiftHeader.viewConteoller(name: vcs[i])
            let nav = MDHNavigationController.init(rootViewController: vc)
            navs.append(nav)
        }
        self.tabBarController.tabBarItemsAttributes = tabbarItems
        /// 添加到tabBarController的子视图
        self.tabBarController.viewControllers = navs
        /// 配置栈底
        AppDelegate.shared()?.navigationControllerStack.push(navs[0])
        self.tabBarController(self.tabBarController, didSelect: navs[0])
    }
    
    //MARK: - ==== UITabBarControllerDelegate
    func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
        #if DEBUG /// 调试模式
        print("viewController   %@  %zd", viewController, viewController.tabBarItem.tag)
        #endif
        AppDelegate.shared()?.navigationControllerStack.popNavigationController()
        AppDelegate.shared()?.navigationControllerStack.push(viewController as! UINavigationController)
        let index = self.tabBarController.selectedIndex
        
        var color_str = "#00BFE5"
        switch index {
        case 1:
            color_str = "#EF769F"
        case 2:
            color_str = "#56D0A9"
        case 3:
            color_str = "#CE5BDC"
        case 4:
            color_str = "#EEAD1F"
        default:
            break
        }
        let tabBarItem = viewController.tabBarItem
        tabBarItem?.setTitleTextAttributes([NSAttributedString.Key.foregroundColor : kColor_MDHHex(color_str)], for: .selected)
        tabBarItem?.setTitleTextAttributes([NSAttributedString.Key.foregroundColor : kColor_MDHHex("#363636")], for: .normal)
    }
}
